package com.prik.decorator.javaio;

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @description: java io 装饰者：将输入流的所有大写字母转为小写
 * @author: prik
 * @create: 2019-07-24
 **/
public class LowerCaseInputStream extends FilterInputStream {

    public LowerCaseInputStream(InputStream inputStream) {
        super(inputStream);
    }

    /**
     * 针对字节
     * @return
     * @throws IOException
     */
    @Override
    public int read() throws IOException {
        int c = super.read();
        return (c == -1 ? c : Character.toLowerCase((char) c));
    }

    /**
     * 针对字节数组
     * @param b
     * @param offset
     * @param len
     * @return
     * @throws IOException
     */
    @Override
    public int read(byte[] b, int offset, int len) throws IOException {
        int result = super.read(b, offset, len);
        for (int i = offset; i < offset + result; i++) {
            b[i] = (byte) Character.toLowerCase((char) b[i]);
        }
        return result;
    }
}